package com.gl.emms.whale.ui;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.util.DialogFactory;
import com.gl.emms.whale.util.SharePreferenceUtil;

public class EnergyAnalysisActivity extends WhaleMonitorActivity implements
OnClickListener{
	private Button titleBackBtn;
	private Button repairButton;;
	private Button repairCallButton;


	private GraphicalView mChartView; //显示图表  
	private LinearLayout layout; //用于绑定图表控件。
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_energyanalysis);
		titleBackBtn = (Button)findViewById(R.id.TitleBackBtn); 
		titleBackBtn.setOnClickListener(this);
		repairButton = (Button)findViewById(R.id.button1); 
		repairButton.setOnClickListener(this);
		repairCallButton = (Button)findViewById(R.id.button2); 
		repairCallButton.setOnClickListener(this);
		layout = (LinearLayout)findViewById(R.id.chart);

		String[] titles = new String[] { "上月能量数据", "本月能量数据", "去年此月能量数据"};
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++) {
			x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 });
		values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
		values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
		int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN};
		PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE};
		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
		}
		setChartSettings(renderer, "能耗分析", "月份", "能量数据", 1, 12, 0, 40, Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);



		View view = ChartFactory.getLineChartView(this, buildDataset(titles, x, values), renderer);
		//view.setBackgroundColor(Color.BLACK);
		view.setOnClickListener(gOCL);
		layout.addView(view,
				new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		EMMSPushManager.setNlbDataRequest(this,"2013-9-25 10", "2013-9-26 10");
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:

			break;
		case R.id.button2:

			break;
		case R.id.TitleBackBtn:
			//用intent启动拨打电话  
			KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,  
					KeyEvent.KEYCODE_BACK);
			finish();
			break;
		default:
			break;
		}
	}
	private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		setRenderer(renderer, colors, styles);
		return renderer;
	}

	private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
		/*
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setPointSize(5f);
        renderer.setMargins(new int[] { 20, 30, 15, 20 });
		 */
		renderer.setClickEnabled(true); //是否可以点击

		renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));

		renderer.setApplyBackgroundColor(true);//设置是否显示背景色  
		renderer.setBackgroundColor(Color.argb(100, 50, 50, 50));//设置背景色  

		renderer.setAxisTitleTextSize(20); //设置轴标题文字的大小  
		renderer.setChartTitleTextSize(30);//?设置整个图表标题文字大小  
		renderer.setLabelsTextSize(25);//设置刻度显示文字的大小(XY轴都会被设置)  
		renderer.setLegendTextSize(20);//图例文字大小  
		renderer.setMargins(new int[] { 50, 70, 50, 40 });//设置图表的外边框(上/左/下/右)  
		renderer.setZoomButtonsVisible(true);//是否显示放大缩小按钮  
		renderer.setPanLimits(new double[] { 1, 12, 0, 40 });
		renderer.setZoomLimits(new double[] { 1, 12, 0, 40 });
		renderer.setPointSize(5);//设置点的大小(图上显示的点的大小和图例中点的大小都会被设置)



		int length = colors.length;
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			r.setDisplayChartValues(true);
			r.setChartValuesTextAlign(Align.RIGHT);
			r.setChartValuesTextSize(20);
			renderer.addSeriesRenderer(r);
		}
	}
	OnClickListener gOCL = new OnClickListener() {

		@Override
		public void onClick(View v) { 
			GraphicalView gv = (GraphicalView) v;
			//将view转换为可以监听的GraphicalView
			SeriesSelection ss = gv.getCurrentSeriesAndPoint();
			//获得被点击的系列和点
			if (ss == null) return ;
			double[] point = new double[]{ss.getXValue(),ss.getValue()};
			//获得当前被点击点的X位置和Y数值
			//获得当前被点击点的坐标位置

			showToask("点击了("+point[0]+","+point[1]+")点\n");
		}
	};
	private void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	private XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset, titles, xValues, yValues, 0);
		return dataset;
	}

	private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) {
		int length = titles.length;
		for (int i = 0; i < length; i++) {
			XYSeries series = new XYSeries(titles[i], scale);
			double[] xV = xValues.get(i);
			double[] yV = yValues.get(i);
			int seriesLength = xV.length;
			for (int k = 0; k < seriesLength; k++) {
				series.add(xV[k], yV[k]);
			}
			dataset.addSeries(series);
		}
	}




	@Override
	public void onReplyReceived(final ReplyBody reply) {
		if (reply.getType().equals("nlbdata_response")) {
			DialogFactory.ToastDialog(EnergyAnalysisActivity.this, "数据更新",
					"成功");
		}
	}
}
